import axios from "axios";
import { getToken } from "@/utils/auth";
import { Message } from "element-ui";
import { MessageBox } from "element-ui";

const env = require("/env");

const service = axios.create({
  baseURL: env[process.env.NODE_ENV]
});

// request interceptor
service.interceptors.request.use(
  (config) => {

    config.headers['token'] = getToken() || ""

    if (config.data != null) {
      config.data.opFrom = 1;
    }

    return config;
  },
  (error) => {
    // do something with request error
    // console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */

  (response) => {
    let url = response.config.url;
    // console.log(url);
    const res = response.data;

    // if (res instanceof ArrayBuffer) {
    //   return res;
    // }
    return res;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
